﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using WisDom.IService;
using WisDom.Models;
using WisDom.Util;
using WisDom.Util.Extension;
using WisDom.Utils;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
using WisDom.DBContext;
using System.Linq.Expressions;
using Microsoft.Extensions.Options;

namespace WisDom.Services
{
    /// <summary>
    /// 统一门户数据源服务
    /// </summary>
    public partial class UPDataSourceService : BaseService<UPDataSource>, IUPDataSourceInterface
    {
        //private ApplicationDbContext _dbContext;
        //private IOptions<DbContextOption> _dbContextOption; 

        public UPDataSourceService(ApplicationDbContext db, IOptions<DbContextOption> dbContextOption) : base(db, dbContextOption)
        {
            //_dbContext = db;
            //this._dbContextOption = db
        }

        #region 公共查询条件
        /// <summary>
        /// 公共的搜索条件
        /// </summary>
        /// <param name="queryJson"></param>
        /// <returns></returns>
        protected override Expression<Func<UPDataSource, bool>> TrueCondition(string queryJson)
        {
            var ext = LinqExtensions.True<UPDataSource>();
            var queryParam = queryJson.ToJObject();
            if (!queryParam["keyword"].IsEmpty())
            {
                string keyword = queryParam["keyword"].ToString();
                ext = ext.And(n => n.DataSourceName.IndexOf(keyword) >= 0);
            }
            return ext;
        }

        #endregion

        #region 获取数据

        /// <summary>
        /// 获取分页的DataTable
        /// </summary> 
        /// <param name="queryJson">查询条件</param>
        /// <returns></returns>
        public Task<DataTable> GetDataTable(string queryJson)
        {
            string sql = "select * from ";
            var queryParam = queryJson.ToJObject();
            if (!queryParam["keyword"].IsEmpty())
            {
                string keyword = queryParam["keyword"].ToString();
            }
            return GetBaseDataTable(sql);
        }
        /// <summary>
        /// 获取分页数据表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="queryJson"></param>
        /// <returns></returns>
        public async Task<DataTable> GetPageDataTable(Pagination pagination, string queryJson)
        {
            string sql = "";
            return await GetBasePageData(pagination, sql);
        }

        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <returns></returns>
        public async Task<UPDataSource> GetEntity(string keyValue)
        {
            return await GetSingle(n => n.DataSourceId == keyValue);
        }
        #endregion

        #region 保存数据
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="keyvalue"></param>
        /// <param name="entity"></param>
        /// <returns></returns>   
        public async Task<bool> SaveForm(string keyValue, UPDataSource entity)
        {
            if (string.IsNullOrEmpty(keyValue))
            {
                return await InsertData(entity);
            }
            else
            {
                return await UpdateData(entity);
            }
        }

        /// <summary>
        /// 移除数据
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public async Task RemoveForm(string keyValue)
        {
            await RemoveData(n => n.DataSourceId == keyValue);
        }


        #endregion

    }

}
